import { useLocation, useNavigate } from 'react-router-dom'
import PdfInvoiceResult from "../models/PdfInvoiceResult.ts";
import { useEffect, useState } from "react";
import InvoicesTable from "./InvoicesTable.tsx";
import store from "../models/store.ts";
import { Button } from "@douyinfe/semi-ui";
import { IconEyeOpened, IconFile, IconPrint } from "@douyinfe/semi-icons";
import PrintView from "./PrintView.tsx";
import './index.css'
import downloadExcel from "./downloadExcel.tsx";

export default () => {
    const navigate = useNavigate();
    const pdfResults = store?.pdfSuccessResults as PdfInvoiceResult[];
    const [openPrintView, setOpenPrintView] = useState(false);
    useEffect(() => {
        if (!pdfResults?.length) {
            navigate('/upload')
        }
    }, pdfResults)
    if (!pdfResults?.length) {
        return null;
    }

    return <>
        <div >
            <Button
                icon={<IconFile />}
                onClick={downloadExcel}
                style={{ width: '120px', marginTop: '0', marginBottom: '20px', height: '40px' }}
                size={'large'}
                type="primary"
                theme="solid"
            >下载Excel</Button >
            &nbsp;
            <Button
                icon={<IconEyeOpened />}
                onClick={() => setOpenPrintView(true)}
                style={{ width: '120px', marginTop: '0', marginBottom: '20px', height: '40px' }}
                size={'large'}
                type="warning"
                theme="solid"
            >打印预览</Button >
        </div >
        <PrintView visible={openPrintView} setVisible={setOpenPrintView} />
        <InvoicesTable pdfInvoices={store.pdfResults} />
        <br />
    </>
}